package it.polimi.it.TravelDream.ejb.eaos;

import it.polimi.it.TravelDream.entities.Purchas;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;


/**
 * Session Bean implementation class PurchaseMgrBean
 */
@Stateless
@LocalBean
public class PurchaseMgrBean implements PurchaseMgr {
	
	@PersistenceContext
    private EntityManager em;
	
	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<PurchasDTO> getPurchasesList(String user){
		System.out.println("*************************");
		return listConverterDTO(em.createQuery(
				"SELECT p FROM Purchas p WHERE p.idbuyer LIKE :user").setParameter("user", user).getResultList());
	}	

	
	private ArrayList<PurchasDTO> listConverterDTO(List<Purchas> purchaseList){
		
	int i;
	ArrayList<PurchasDTO> newList = new ArrayList<PurchasDTO>();
	for( i=0;i<purchaseList.size();i++){
		newList.add(convertToDTO(purchaseList.get(i)));
	}
	
	return newList;
		
	}
		
	
	private PurchasDTO convertToDTO(Purchas purchase){
		PurchasDTO purchasDTO = new PurchasDTO();
		purchasDTO.setIdbuyer(purchase.getIdbuyer());
		purchasDTO.setIdproduct(purchase.getIdproduct());
		return purchasDTO;
	}
	
	
    /**
     * Default constructor. 
     */
    public PurchaseMgrBean() {
        // TODO Auto-generated constructor stub
    }
    
}

